package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfMerchantInfo;
import com.ocom.common.entity.company.EntityCompanyInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.pay.MachMerchPointInfoVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoXfMerchantInfo;
import com.ocom.payhandle.service.CompanyInfoService;
import com.ocom.payhandle.service.XfMerchantInfoService;
import com.ocom.payhandle.vo.DeviceInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class XfMerchantInfoServiceImpl extends ServiceImpl<IDaoXfMerchantInfo, EntityXfMerchantInfo> implements XfMerchantInfoService {

    @Autowired
    CompanyInfoService companyInfoService;

    @Autowired
    private IDaoXfMerchantInfo merchantInfo;

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyID")
    @Override
    public EntityXfMerchantInfo selectConfigure(Long companyID, DeviceInfoVo deviceInfoVo) {
        QueryWrapper<EntityXfMerchantInfo> query = Wrappers.query();
        query.select("company_id","user_name","password");
        query.eq("com_id",companyID);
        Long merchId=-1l;
        if (deviceInfoVo!=null){
            if (deviceInfoVo.getPointId()!=null && deviceInfoVo.getPointId()!=0){
                if (deviceInfoVo.getMerchId()!=null && deviceInfoVo.getMerchId()!=0){
                    merchId=deviceInfoVo.getMerchId();
                }
            }
        }
        if (merchId==-1l){
            query.eq("parent_merch_id",-1l);
        }else {
            query.eq("merch_id",merchId);
        }
        query.orderByDesc("update_time");
        query.last("limit 1");
        return merchantInfo.selectOne(query);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityXfMerchantInfo> selectComAcc(Long comId) {
        QueryWrapper<EntityXfMerchantInfo> query = Wrappers.query();
        query.eq("com_id",comId);
        return merchantInfo.selectList(query);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityXfMerchantInfo selectParentMerchId(Long companyId, int parentMerchId) {
        QueryWrapper<EntityXfMerchantInfo> query = Wrappers.query();
        query.select("parent_merch_id","withhold_submerch");
        query.eq("com_id",companyId);
        query.eq("merch_id",parentMerchId);
        return merchantInfo.selectOne(query);
    }

    @Override
    public Result GetMerchInfoByMachNo(Long comId, String machNo) {
        MachMerchPointInfoVo machMerchPointInfo;

        EntityCompanyInfo entityCompanyInfo = companyInfoService.GetCompanyInfo(comId);
        if(entityCompanyInfo==null){
            return ResultUtil.error(ResultEnum.Err_301);
        }
        MachMerchPointInfoVo machMerchPointInfoVo = merchantInfo.getMerchPointInfo(comId,machNo);
        if(machMerchPointInfoVo ==null ){
            return ResultUtil.error(ResultEnum.Err_3021);
        }
        machMerchPointInfoVo.setCompanyName(entityCompanyInfo.getComName());
        if(machMerchPointInfoVo.getMerchId()==0){
            machMerchPointInfoVo.setMerchName(entityCompanyInfo.getComName());
        }
        return ResultUtil.success(machMerchPointInfoVo);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyId",isMaster = MasterSlaveEnum.SLAVE)
    public EntityXfMerchantInfo getMerchInfo(Long companyId, Integer merchId) {
        QueryWrapper<EntityXfMerchantInfo> query = Wrappers.query();
        query.select("merch_name");
        query.eq("com_id",companyId);
        query.eq("merch_id",merchId);
        return merchantInfo.selectOne(query);
    }
}
